package jdbc.dao.vente;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import metier.vente.Vente;
import jdbc.DAO;
import jdbc.dao.compte.UtilisateurDAO;
import jdbc.dao.jeu.BoiteDeJeuDAO;

public class VenteDAO extends DAO<Vente> {

	@Override
	public Vente find(int id) {
		Vente vente=null ;
		try {
			ResultSet result = DAO.connect
					.createStatement(
							ResultSet.TYPE_SCROLL_INSENSITIVE,
							ResultSet.CONCUR_UPDATABLE
							).executeQuery(
									"SELECT * FROM VENTE WHERE IdVente = " + id
									);
			if(result.first()){
				vente = new Vente(
						id,
						result.getFloat("Prix"),
						result.getDate("DateMiseEnVente"),
						result.getDate("DateVente"),
						new BoiteDeJeuDAO().find(result.getInt("IdBoiteDeJeu")),
						new UtilisateurDAO().find(result.getInt("IdUtilisateur"))
						);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return vente;
	}

	@Override
	public Vente create(Vente obj) {
		try {
			ResultSet result = DAO .connect
					.createStatement(
							ResultSet.TYPE_SCROLL_INSENSITIVE,
							ResultSet.CONCUR_UPDATABLE
							).executeQuery(
									"SELECT vente_id_seq.nextval as IdVente from dual"
									);
			if(result.first()){
				java.sql.Date dateMiseVenteSql = new java.sql.Date(obj.getDateMiseEnVente().getTime());
				java.sql.Date dateVenteSql = new java.sql.Date(obj.getDateVente().getTime());
				int id = result.getInt("IdVente");
				PreparedStatement prepare = DAO .connect
						.prepareStatement(
								"INSERT INTO Vente (IdVente, IdUtilisateur, Prix, IdBoiteDeJeu, DateMiseEnVente, DateVente) VALUES(?,?,?,?,TO_DATE(' "+dateMiseVenteSql+" ','YYYY-MM-DD'),TO_DATE(' "+dateVenteSql+" ','YYYY-MM-DD'))"
								);
	
				prepare.setInt(1, id);
				prepare.setInt(2,obj.getUTILISATEUR().getID_UTILISATEUR());
				prepare.setFloat(3,obj.getPrix());
				prepare.setInt(4, obj.getBDJ().getID_BOITE_DE_JEU());
				prepare.executeUpdate();
				obj = this.find(id);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public Vente update(Vente obj) {
		try {
			DAO.connect
			.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE
					).executeUpdate(
							"UPDATE Vente SET IdUtilisateur = '" + obj.getUTILISATEUR().getID_UTILISATEUR() + "'"+	
									", Prix = '"+ obj.getPrix() +"'"+
									", IdBoiteDeJeu = '"+ obj.getBDJ().getID_BOITE_DE_JEU() +"'"+
									", DateMiseEnvente = TO_DATE(' "+ new java.sql.Date(obj.getDateMiseEnVente().getTime()) +" ','YYYY-MM-DD') + "+
									", Datevente = TO_DATE(' "+ new java.sql.Date(obj.getDateVente().getTime()) +" ','YYYY-MM-DD') + "+
									" WHERE IdVente = " + obj.getID_VENTE()
							);
			obj = this.find(obj.getID_VENTE());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public void delete(Vente obj) {
		try {
			DAO.connect
			.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE
					).executeUpdate(
							"DELETE FROM Vente WHERE IdVente = " + obj.getID_VENTE()
							);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
